package com.songsong.myblog2.service.impl;

import net.sf.json.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.songsong.myblog2.component.StringAndArray;
import com.songsong.myblog2.domain.Article;
import com.songsong.myblog2.mapper.ArticleMapper;
import com.songsong.myblog2.service.ArticleService;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "articleService")
public class ArticleServiceImpl implements ArticleService {
    /*注入articleMapper*/
    @Resource(name = "articleMapper")
    private ArticleMapper articleMapper;

    /**
     * 添加文章
     *
     * @param article
     */
    @Override
    public void addArticle(Article article) {
        /*调用dao*/
        articleMapper.addArticle(article);
    }

    /**
     * 获取所有的文章
     *
     * @param rows
     * @param pageNo
     * @return
     */
    @Override
    public JSONArray findAllArticles(String rows, String pageNo) {
        int pageNum = Integer.parseInt(pageNo);
        int pageSize = Integer.parseInt(rows);

        PageHelper.startPage(pageNum, pageSize);
        List<Article> articles = articleMapper.findAllArticles();
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        List<Map<String, Object>> newArticles = new ArrayList<>();
        Map<String, Object> map;

        for (Article article : articles) {
            map = new HashMap<>();
            map.put("thisArticleUrl", article.getArticleUrl());
            map.put("articleTags", StringAndArray.stringToArray(article.getArticleTags()));
            map.put("articleTitle", article.getArticleTitle());
            map.put("articleType", article.getArticleType());
            map.put("publishDate", article.getArticleDate());
            map.put("originalAuthor", article.getArticleAuthor());
            map.put("articleCategories", article.getArticleCategories());
            map.put("articleTabloid", article.getArticleTabloid());
            map.put("articleImg", article.getArticleImg());
            map.put("likes", article.getArticleLikes());
            newArticles.add(map);
        }
        JSONArray jsonArray = JSONArray.fromObject(newArticles);
        Map<String, Object> thisPageInfo = new HashMap<>();
        thisPageInfo.put("pageNum", pageInfo.getPageNum());
        thisPageInfo.put("pageSize", pageInfo.getPageSize());
        thisPageInfo.put("total", pageInfo.getTotal());
        thisPageInfo.put("pages", pageInfo.getPages());
        thisPageInfo.put("isFirstPage", pageInfo.isIsFirstPage());
        thisPageInfo.put("isLastPage", pageInfo.isIsLastPage());

        jsonArray.add(thisPageInfo);
        return jsonArray;
    }

    /**
     * 根据id查询文章
     *
     * @param id
     * @return
     */
    @Override
    public Article findArticleById(long id) {
        Article article = articleMapper.findArticleById(id);
        return article;
    }

    /**
     * 获取文章数量
     *
     * @return
     */
    @Override
    public int getArticleNum() {
        int num = articleMapper.getArticleNum();
        return num;
    }

    /**
     * 获取最新的五篇文章
     *
     * @param rows
     * @param pageNum
     * @return
     */
    @Override
    public JSONObject findFiveNewArticle(int rows, int pageNum) {
        JSONObject returnJson = new JSONObject();
        PageHelper.startPage(pageNum, rows);
        List<Article> fivArticles = articleMapper.findFiveNewArticle();
        PageInfo<Article> pageInfo = new PageInfo<>(fivArticles);

        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject;

        for (Article article : fivArticles) {
            jsonObject = new JSONObject();
            jsonObject.put("articleUrl", article.getArticleUrl());
            jsonObject.put("articleDate", article.getArticleDate());
            jsonObject.put("articleTitle", article.getArticleTitle());
            jsonArray.add(jsonObject);
        }
        returnJson.put("status", 200);
        returnJson.put("result", jsonArray);
        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum", pageInfo.getPageNum());
        pageJson.put("pageSize", pageInfo.getPageSize());
        pageJson.put("total", pageInfo.getTotal());
        pageJson.put("pages", pageInfo.getPages());
        pageJson.put("isFirstPage", pageInfo.isIsFirstPage());
        pageJson.put("isLastPage", pageInfo.isIsLastPage());
        returnJson.put("pageInfo", pageJson);
        return returnJson;
    }

    /**
     * 获取最热的五篇文章
     *
     * @param rows
     * @param pageNum
     * @return
     */
    @Override
    public JSONObject findFiveHotArticle(int rows, int pageNum) {
        JSONObject returnJson = new JSONObject();
        PageHelper.startPage(pageNum, rows);
        List<Article> fivArticles = articleMapper.findFiveHotArticle();
        PageInfo<Article> pageInfo = new PageInfo<>(fivArticles);

        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject;

        for (Article article : fivArticles) {
            jsonObject = new JSONObject();
            jsonObject.put("articleUrl", article.getArticleUrl());
            jsonObject.put("articleDate", article.getArticleDate());
            jsonObject.put("articleTitle", article.getArticleTitle());
            jsonArray.add(jsonObject);
        }
        returnJson.put("status", 200);
        returnJson.put("result", jsonArray);
        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum", pageInfo.getPageNum());
        pageJson.put("pageSize", pageInfo.getPageSize());
        pageJson.put("total", pageInfo.getTotal());
        pageJson.put("pages", pageInfo.getPages());
        pageJson.put("isFirstPage", pageInfo.isIsFirstPage());
        pageJson.put("isLastPage", pageInfo.isIsLastPage());
        returnJson.put("pageInfo", pageJson);
        return returnJson;
    }

    /*
     * hot+1
     * */
    @Override
    public void updateArticleHot(int id) {
        articleMapper.updateArticleHot(id);
    }

    /**
     * 分页获得归档文章
     *
     * @param archive 归档日期
     * @param rows    一页大小
     * @param pageNum 页数
     * @return
     */
    @Override
    public JSONObject findArticleByArchive(String archive, int rows, int pageNum) {
        List<Article> articles;
        PageInfo<Article> pageInfo;
        JSONArray articleJsonArray = new JSONArray();
        String showMonth = "hide";
        PageHelper.startPage(pageNum, rows);
        if ("".equals(archive)) {
            articles = articleMapper.findAllArticles();
        } else {
            articles = articleMapper.findArticleByArchive(archive);
            showMonth = "show";
        }
        pageInfo = new PageInfo<>(articles);

        articleJsonArray = timeLineReturn(articleJsonArray, articles);

        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum", pageInfo.getPageNum());
        pageJson.put("pageSize", pageInfo.getPageSize());
        pageJson.put("total", pageInfo.getTotal());
        pageJson.put("pages", pageInfo.getPages());
        pageJson.put("isFirstPage", pageInfo.isIsFirstPage());
        pageJson.put("isLastPage", pageInfo.isIsLastPage());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 200);
        jsonObject.put("result", articleJsonArray);
        jsonObject.put("pageInfo", pageJson);
        jsonObject.put("articleNum", articleMapper.getArticleNum());
        jsonObject.put("showMonth", showMonth);
        return jsonObject;
    }

    /**
     * 根据标签获取文章
     *
     * @param rows
     * @param pagenum
     * @param tagName
     * @return
     */
    @Override
    public JSONArray findArticlesByTag(String rows, String pagenum, String tagName) {

        int pageNum = Integer.parseInt(pagenum);
        int pageSize = Integer.parseInt(rows);
        tagName = "%" + tagName + "%";
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articles = articleMapper.findArticlesByTag(tagName);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        List<Map<String, Object>> newArticles = new ArrayList<>();
        Map<String, Object> map;

        for (Article article : articles) {
            map = new HashMap<>();
            map.put("thisArticleUrl", article.getArticleUrl());
            map.put("articleTags", StringAndArray.stringToArray(article.getArticleTags()));
            map.put("articleTitle", article.getArticleTitle());
            map.put("articleType", article.getArticleType());
            map.put("publishDate", article.getArticleDate());
            map.put("originalAuthor", article.getArticleAuthor());
            map.put("articleCategories", article.getArticleCategories());
            map.put("articleTabloid", article.getArticleTabloid());
            map.put("articleImg", article.getArticleImg());
            map.put("likes", article.getArticleLikes());
            newArticles.add(map);
        }
        JSONArray jsonArray = JSONArray.fromObject(newArticles);
        Map<String, Object> thisPageInfo = new HashMap<>();
        thisPageInfo.put("pageNum", pageInfo.getPageNum());
        thisPageInfo.put("pageSize", pageInfo.getPageSize());
        thisPageInfo.put("total", pageInfo.getTotal());
        thisPageInfo.put("pages", pageInfo.getPages());
        thisPageInfo.put("isFirstPage", pageInfo.isIsFirstPage());
        thisPageInfo.put("isLastPage", pageInfo.isIsLastPage());

        jsonArray.add(thisPageInfo);
        return jsonArray;
    }

    /**
     * 分页获取用户的文章
     *
     * @param username
     * @param rows
     * @param pagenum
     * @return
     */
    @Override
    public JSONArray findArticlesByUser(String username, String rows, String pagenum) {
        int pageNum = Integer.parseInt(pagenum);
        int pageSize = Integer.parseInt(rows);

        PageHelper.startPage(pageNum, pageSize);
        List<Article> articlesByUser = articleMapper.findArticlesByUser(username);
        PageInfo<Article> pageInfo = new PageInfo<>(articlesByUser);
        List<Map<String, Object>> newArticles = new ArrayList<>();
        Map<String, Object> map;

        for (Article article : articlesByUser) {
            map = new HashMap<>();
            map.put("thisArticleUrl", article.getArticleUrl());
            map.put("articleTitle", article.getArticleTitle());
            map.put("articleTabloid", article.getArticleTabloid());
            map.put("articleImg", article.getArticleImg());
            newArticles.add(map);
        }
        JSONArray jsonArray = JSONArray.fromObject(newArticles);
        Map<String, Object> thisPageInfo = new HashMap<>();
        thisPageInfo.put("pageNum", pageInfo.getPageNum());
        thisPageInfo.put("pageSize", pageInfo.getPageSize());
        thisPageInfo.put("total", pageInfo.getTotal());
        thisPageInfo.put("pages", pageInfo.getPages());
        thisPageInfo.put("isFirstPage", pageInfo.isIsFirstPage());
        thisPageInfo.put("isLastPage", pageInfo.isIsLastPage());

        jsonArray.add(thisPageInfo);
        return jsonArray;
    }

    //获取文章管理
    @Override
    public JSONObject getArticleManagement(String username, int rows, int pageNum) {
        PageHelper.startPage(pageNum, rows);
        List<Article> articles = articleMapper.getArticleManagement(username);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        JSONArray returnJsonArray = new JSONArray();
        JSONObject returnJson = new JSONObject();
        JSONObject articleJson;
        for (Article article : articles) {
            articleJson = new JSONObject();
            articleJson.put("articleId", article.getArticleId());
            articleJson.put("articleAuthor", article.getArticleAuthor());
            articleJson.put("articleTitle", article.getArticleTitle());
            articleJson.put("articleCategories", article.getArticleCategories());
            articleJson.put("articleDate", article.getArticleDate());
            articleJson.put("articleHot", article.getArticleHot());
            articleJson.put("articleUrl", article.getArticleUrl());
            returnJsonArray.add(articleJson);
        }
        returnJson.put("status", 200);
        returnJson.put("result", returnJsonArray);
        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum", pageInfo.getPageNum());
        pageJson.put("pageSize", pageInfo.getPageSize());
        pageJson.put("total", pageInfo.getTotal());
        pageJson.put("pages", pageInfo.getPages());
        pageJson.put("isFirstPage", pageInfo.isIsFirstPage());
        pageJson.put("isLastPage", pageInfo.isIsLastPage());
        returnJson.put("pageInfo", pageJson);
        return returnJson;
    }

    /**
     * 封装时间线中数据成JsonArray形式
     */
    private JSONArray timeLineReturn(JSONArray articleJsonArray, List<Article> articles) {
        JSONObject articleJson;
        for (Article article : articles) {
            String[] tagsArray = StringAndArray.stringToArray(article.getArticleTags());
            articleJson = new JSONObject();
            articleJson.put("articleId", article.getArticleId());
            articleJson.put("originalAuthor", article.getArticleAuthor());
            articleJson.put("articleTitle", article.getArticleTitle());
            articleJson.put("articleCategories", article.getArticleCategories());
            articleJson.put("publishDate", article.getArticleDate());
            articleJson.put("articleUrl", article.getArticleUrl());
            articleJson.put("articleTags", tagsArray);
            articleJsonArray.add(articleJson);
        }
        return articleJsonArray;
    }

    /**
     * 获取修改文章
     *
     * @param article
     * @param articleTags
     * @return
     */
    @Override
    public JSONObject getDraftArticle(Article article, String[] articleTags) {
        JSONObject returnJson = new JSONObject();
        returnJson.put("id", article.getArticleId());
        returnJson.put("articleTitle", article.getArticleTitle());
        returnJson.put("articleType", article.getArticleType());
        returnJson.put("articleCategories", article.getArticleCategories());
        returnJson.put("articleUrl", article.getArticleUrl());
        returnJson.put("articleContent", article.getArticleContent());
        returnJson.put("articleTags", articleTags);
        return returnJson;
    }

    /**
     * 修改文章
     *
     * @param article
     * @return
     */
    @Override
    public JSONObject updateArticleById(Article article) {
        Article a = articleMapper.findArticleById(article.getArticleId());
        articleMapper.updateArticleById(article);
        JSONObject articleReturn = new JSONObject();
        articleReturn.put("status", 200);
        articleReturn.put("articleTitle", article.getArticleTitle());
        articleReturn.put("updateDate", a.getArticleDate());
        articleReturn.put("author", article.getArticleAuthor());
        articleReturn.put("articleUrl", a.getArticleUrl());
        return articleReturn;
    }

    /**
     * 删除文章
     *
     * @param id 文章id
     * @return
     */
    @Override
    public int deleteArticle(long id) {
           /* Article deleteArticle = articleMapper.findAllArticleId(id);
            articleMapper.updateLastOrNextId("lastArticleId", deleteArticle.getLastArticleId(), deleteArticle.getNextArticleId());
            articleMapper.updateLastOrNextId("nextArticleId", deleteArticle.getNextArticleId(), deleteArticle.getLastArticleId());*/
        //删除本篇文章
        articleMapper.deleteByArticleId(id);
            /*//删除与该文章有关的所有文章点赞记录、文章评论、文章评论记录
            commentService.deleteCommentByArticleId(deleteArticle.getArticleId());
            commentLikesRecordService.deleteCommentLikesRecordByArticleId(deleteArticle.getArticleId());
            articleLikesRecordService.deleteArticleLikesRecordByArticleId(deleteArticle.getArticleId());*/
        return 1;
    }

    @Override
    public JSONObject getArticleManagementAdmin(int rows, int pageNum) {

        PageHelper.startPage(pageNum, rows);
        List<Article> articles = articleMapper.getArticleManagementAdmin();
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        JSONArray returnJsonArray = new JSONArray();
        JSONObject returnJson = new JSONObject();
        JSONObject articleJson;
        for (Article article : articles) {
            articleJson = new JSONObject();
            articleJson.put("articleId", article.getArticleId());
            articleJson.put("articleAuthor", article.getArticleAuthor());
            articleJson.put("articleTitle", article.getArticleTitle());
            articleJson.put("articleCategories", article.getArticleCategories());
            articleJson.put("articleDate", article.getArticleDate());
            articleJson.put("articleHot", article.getArticleHot());
            articleJson.put("articleUrl", article.getArticleUrl());
            returnJsonArray.add(articleJson);
        }
        returnJson.put("status", 200);
        returnJson.put("result", returnJsonArray);
        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum", pageInfo.getPageNum());
        pageJson.put("pageSize", pageInfo.getPageSize());
        pageJson.put("total", pageInfo.getTotal());
        pageJson.put("pages", pageInfo.getPages());
        pageJson.put("isFirstPage", pageInfo.isIsFirstPage());
        pageJson.put("isLastPage", pageInfo.isIsLastPage());
        returnJson.put("pageInfo", pageJson);
        return returnJson;
    }

    @Override
    public JSONObject findArticleByCategory(String category, int rows, int pageNum) {

        List<Article> articles;
        PageInfo<Article> pageInfo;
        JSONArray articleJsonArray = new JSONArray();
        PageHelper.startPage(pageNum, rows);
        if("".equals(category)){
            articles = articleMapper.findAllArticles();
            category = "全部分类";
        } else {
            articles = articleMapper.findArticleByCategory(category);
        }
        pageInfo = new PageInfo<>(articles);

        articleJsonArray = timeLineReturn(articleJsonArray, articles);

        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum",pageInfo.getPageNum());
        pageJson.put("pageSize",pageInfo.getPageSize());
        pageJson.put("total",pageInfo.getTotal());
        pageJson.put("pages",pageInfo.getPages());
        pageJson.put("isFirstPage",pageInfo.isIsFirstPage());
        pageJson.put("isLastPage",pageInfo.isIsLastPage());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",200);
        jsonObject.put("result",articleJsonArray);
        jsonObject.put("category",category);
        jsonObject.put("pageInfo",pageJson);

        return jsonObject;
    }

    @Override
    public JSONArray findArticlesBySearch(String keyboard, String rows, String pageNum) {
        int pageuum = Integer.parseInt(pageNum);
        int pageSize = Integer.parseInt(rows);

        PageHelper.startPage(pageuum, pageSize);
        List<Article> articlesByUser = articleMapper.findArticlesBySearch(keyboard);
        PageInfo<Article> pageInfo = new PageInfo<>(articlesByUser);
        List<Map<String, Object>> newArticles = new ArrayList<>();
        Map<String, Object> map;

        for (Article article : articlesByUser) {
            map = new HashMap<>();
            map.put("thisArticleUrl", article.getArticleUrl());
            map.put("articleTitle", article.getArticleTitle());
            map.put("articleTabloid", article.getArticleTabloid());
            map.put("articleImg", article.getArticleImg());
            newArticles.add(map);
        }
        JSONArray jsonArray = JSONArray.fromObject(newArticles);
        Map<String, Object> thisPageInfo = new HashMap<>();
        thisPageInfo.put("pageNum", pageInfo.getPageNum());
        thisPageInfo.put("pageSize", pageInfo.getPageSize());
        thisPageInfo.put("total", pageInfo.getTotal());
        thisPageInfo.put("pages", pageInfo.getPages());
        thisPageInfo.put("isFirstPage", pageInfo.isIsFirstPage());
        thisPageInfo.put("isLastPage", pageInfo.isIsLastPage());

        jsonArray.add(thisPageInfo);
        return jsonArray;
    }
}
